package com.yangjun.dao.impl.usermanage;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


@Repository
public class ExcelRoleManagerDaoImpl {
	
	
	
	@Autowired
	@Resource(name = "bikadJdbcTemplate")
	private JdbcTemplate jdbc;

	
	/**
	 * 添加角色
	 * @param rolename_ch
	 * @param pid
	 * @return
	 */
	public boolean addRole(String rolename_ch,Integer pid){
		
		String query="select id from dv_kad_excel_role where rolename_ch='"+rolename_ch+"'";
		List<Map<String, Object>> queryForList = jdbc.queryForList(query);
		if(queryForList!=null && queryForList.size()>0){
			return false;
		}
		
		Object[] sz = {rolename_ch,pid};
		String sql = "insert into dv_kad_excel_role"
				+ "(rolename_ch,pid, status,updatetime) "
				+ "values (?,?,1,sysdate)";
		int update = jdbc.update(sql, sz);
		System.out.println("return update value--"+update);
		if(update>0){
			return true;
		}
		return false;
	}
	
	
	
	/**
	 * 修改角色
	 * @param rolename_ch
	 * @param pid
	 * @param id
	 * @return
	 */
	public boolean updateRole(String rolename_ch,Integer pid,Integer id){
		Object[] sz = {rolename_ch,pid,id};
		String sql = "update dv_kad_excel_role set rolename_ch=?,pid=?,updatetime=sysdate "
				+ "where id=? ";
		int update = jdbc.update(sql, sz);
		System.out.println("return update value--"+update);
		if(update>0){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 修改角色状态
	 * @param status
	 * @param id
	 * @return
	 */
	public boolean updateRoleStatus(Integer status,Integer id){
		Object[] sz = {status,id};
		String sql = "update dv_kad_excel_role set status=?,updatetime=sysdate "
				+ "where id=? ";
		int update = jdbc.update(sql, sz);
		System.out.println("return update value--"+update);
		if(update>0){
			return true;
		}
		return false;
	}
	
	/**
	 * 角色添加报表
	 * @param roleid
	 * @param excel_id
	 * @param field
	 * @param type
	 * @return
	 */
	public boolean addRoleReport(Integer roleid,String excel_id,String field,Integer type,String dim){
		
		String query="select * from dv_kad_excel_role_report where roleid="+roleid+" "
				+ "and excel_id='"+excel_id+"'";
		List<Map<String, Object>> queryForList = jdbc.queryForList(query);
		if(queryForList!=null && queryForList.size()>0){
			return false;
		}
		Object[] sz = {roleid,excel_id,field,type,dim};
		String sql = "insert into dv_kad_excel_role_report "
				+ "(roleid,excel_id,field,updatetime,type,dim,status) "
				+ "values (?,?,?,sysdate,?,?,1)";
		int update = jdbc.update(sql, sz);
		System.out.println("return update value--"+update);
		if(update>0){
			return true;
		}
		return false;
	}
	
	/**
	 * 修改用户报表
	 * @param id
	 * @param field
	 * @param type
	 * @return
	 */
	public boolean updateRoleReport(Integer id,String field,Integer type,String dim){
		
		Object[] sz = {field,type,dim,id};
		String sql = "update dv_kad_excel_role_report set field=?,type=?,dim=?,updatetime=sysdate where id=?";
		int update = jdbc.update(sql, sz);
		System.out.println("return update value--"+update);
		if(update>0){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 修改角色报表状态
	 * @param status
	 * @param id
	 * @return
	 */
	public boolean updateRoleReportStatu(Integer status,Integer id){
		Object[] sz = {status,id};
		String sql = "update dv_kad_excel_role_report set status=?,updatetime=sysdate "
				+ "where id=? ";
		int update = jdbc.update(sql, sz);
		System.out.println("return update value--"+update);
		if(update>0){
			return true;
		}
		return false;
	}
	
	
	
	/**
	 * 给用户添加角色
	 * @param username
	 * @param realname
	 * @param roleid
	 * @param auth_username
	 * @param auth_realname
	 * @return
	 */
	public boolean addUserRole(Integer userid,String username,String realname,Integer roleid,String auth_username,String auth_realname){
		Object[] sz = {userid,username,realname,roleid,auth_username,auth_realname};
		
		String query="select * from dv_kad_excel_user_role where userid="+userid+" and roleid="+roleid+"";
		List<Map<String, Object>> queryForList = jdbc.queryForList(query);
		if(queryForList!=null && queryForList.size()>0){
			return false;
		}
		
		
		String sql = "insert into dv_kad_excel_user_role "
				+ "(userid,username,realname,roleid,auth_username,auth_realname,status,updatetime) "
				+ "values (?,?,?,?,?,?,1,sysdate)";
		int update = jdbc.update(sql, sz);
		System.out.println("return update value--"+update);
		if(update>0){
			return true;
		}
		return false;
	}	
	
	
	/**
	 * 修改用户角色
	 * @param username
	 * @param realname
	 * @param roleid
	 * @param auth_username
	 * @param auth_realname
	 * @param id
	 * @return
	 */
	public boolean updateUserRole(String realname,Integer roleid,String auth_username,String auth_realname,Integer id){
		Object[] sz = {realname,roleid,auth_username,auth_realname,id};
		String sql = "update  dv_kad_excel_user_role "
				+ "set realname=?,roleid=?,auth_username=?,auth_realname=?,updatetime=sysdate "
				+ "where id=?";
		int update = jdbc.update(sql, sz);
		System.out.println("return update value--"+update);
		if(update>0){
			return true;
		}
		return false;
	}	
	
	
	/**
	 * 修改用户角色状态
	 * @param status
	 * @param id
	 * @return
	 */
	public boolean updateUserRoleStatu(Integer status,Integer id){
		Object[] sz = {status,id};
		String sql = "update dv_kad_excel_user_role set status=?,updatetime=sysdate "
				+ "where id=? ";
		int update = jdbc.update(sql, sz);
		System.out.println("return update value--"+update);
		if(update>0){
			return true;
		}
		return false;
	}
	
	
}
